package com.article.service.impl;

import com.article.entity.ApiResult;
import com.article.entity.model.Article;
import com.article.entity.model.ArticleExample;
import com.article.entity.param.ArticleParam;
import com.article.mapper.ArticleMapper;
import com.article.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public ApiResult addArticle(Article param) {
        int affected = articleMapper.insertSelective(param);
        if (affected > 0 ){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }

    @Override
    public ApiResult deleteArticleById(Article param) {
        int affected = articleMapper.deleteByPrimaryKey(param.getId());
        if (affected > 0 ){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }

    @Override
    public ApiResult updateArticleById(Article param) {
        int affected = articleMapper.updateByPrimaryKeySelective(param);
        if (affected > 0 ){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }

    @Override
    public ApiResult selectArticleList(ArticleParam param) {
        ArticleExample articleExample = new ArticleExample();
        articleExample.setOrderByClause("create_time asc");
        ArticleExample.Criteria criteria = articleExample.createCriteria();
        List<Article> articles = articleMapper.selectByExample(articleExample);
        return ApiResult.ok(articles);
    }
}
